import {hrAjax as proFetch} from 'src/hrpub/common/utils/utils';
import getOnePsnInfo from "./getOnePsnInfo";

const urls = {
    'add': '/nccloud/hrhi/blacklist/BlackListAddAction.do',
    'edit': '/nccloud/hrhi/blacklist/BlackListEditAction.do'
};

export default function initCardForm() {
    const {page} = this.state;
    if (page === 'add') {
        setCardStatus.call(this);
    } else if (page === 'edit') {
        getOnePsnInfo.call(this, () => {
            setCardStatus.call(this)
        });
    } else {
        const {form} = this.props;
        form.setFormStatus('blackcard', 'browse');
        getOnePsnInfo.call(this);
    }
}

function setCardStatus() {
    const {page, orgVal, pk_psndoc_bad, pk_psndoc} = this.state;
    const {form} = this.props;
    let postData = {
        pk_org: orgVal && orgVal.refpk,
        pk_psndoc_bad
    };
    if (page === 'edit') {
        postData.pk_psndoc = pk_psndoc;
    }
    return proFetch({
        url: urls[page],
        body: postData,
    })
        .then((res) => {
            if (res.success) {
                for (let key in res.data.enableItem) {
                    if (res.data.enableItem.hasOwnProperty(key)) res.data.enableItem[key] = !res.data.enableItem[key];
                }
                form.setFormStatus('blackcard', 'edit');
                if (page === 'add') {
                    form.setAllFormValue({'blackcard': res.data.form.blackcard});
                    if (res.data.form.blackcard.rows[0].values.cause.value == null) {
                        form.setFormItemsValue('blackcard', {'cause': {value: '', display: ''}});
                    }
                }
                form.setFormItemsDisabled('blackcard', res.data.enableItem);
                form.setFormItemsVisible('blackcard', res.data.visiableItem);
            }
        });
}
